package org.example.database.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.database.DTO.UserLoginDTO;
import org.example.database.DTO.UserRegisterDTO;
import org.example.database.VO.UserLoginVO;
import org.example.database.entity.User;
import org.example.database.result.Result;
import org.example.database.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        userService.register(userRegisterDTO);
        return Result.success();
    }

    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);
        User user = userService.login(userLoginDTO);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .accountNumber(user.getAccountNumber())
                .userType(user.getUserType())
                .build();

        return Result.success(userLoginVO);
    }
}